package top.yohann.study.common.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import top.yohann.study.common.annotion.AuthToken;
import top.yohann.study.common.common.Result;
import top.yohann.study.common.service.AliyunService;

/**
 * @author Yeung Wah
 * @since 2021/11/21
 */
@Api("阿里云服务")
@RestController
@RequestMapping("/aliyun")
@AuthToken
public class AliyunController {

    @Autowired
    private AliyunService aliyunService;

    @ApiOperation("上传文件")
    @PostMapping("uploadImage")
    public Result<String> uploadImage(@ApiParam("照片") MultipartFile file) {
        String url = aliyunService.uploadFile(file);
        return Result.success(url);
    }

    @ApiOperation("发送验证码")
    @GetMapping("sendCode/{phone}")
    public Result<String> sendCode(@ApiParam("手机号") @PathVariable String phone) {
        String code = aliyunService.sendCode(phone);
        return Result.success(code);
    }

    @ApiOperation("校验验证码")
    @GetMapping("checkCode")
    public Result<String> checkCode(@ApiParam("手机号") @RequestParam String phone, @ApiParam("验证码") @RequestParam String code) {
        boolean checkCode = aliyunService.checkCode(phone, code);
        return Result.state(checkCode);
    }
}